import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import cookies from 'vue-cookies'
import axios from "axios";
import store from '@/store'
import {message} from 'ant-design-vue'
import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'
import User from "@/views/personnel/User";
import Role from "@/views/personnel/Role";
import Department from "@/views/personnel/Department";
import Permission from "@/views/system/Permission";
import ToDoTasks from "@/views/myself/ToDoTasks";
import LeaveBill from "@/views/myself/LeaveBill";
import ReimbursementBill from "@/views/myself/ReimbursementBill";
import ProductList from "@/views/product/ProductList";
import Conceptual from "@/views/product/Conceptual";
import DevelopedCenter from "@/views/developed/DevelopedCenter";
import ProducePlan from "@/views/business/ProducePlan";
import salesOrder from "@/views/business/salesOrder";
import OutboundBill from "@/views/business/OutboundBill";
import Salary from "@/views/finance/Salary";
import FinanceInfo from "@/views/finance/FinanceInfo";
import Resign from "@/views/myself/Resign";
import HomeContent from "@/views/HomeContent";
import MyDepartment from "@/views/myself/MyDepartment";

const routes = [
    {
        path: '/',
        name: 'login',
        component: Login,
        meta: {requiredLogin: false}
    },
    {
        path: '/home',
        name: 'home',
        component: HomeView,
        children: [
            {
                path: '',
                name: 'homeContent',
                component: HomeContent
            },
            {
                path: '/personnel/user',
                name: 'user',
                component: User
            },
            {
                path: '/personnel/role',
                name: 'role',
                component: Role
            },
            {
                path: '/personnel/department',
                name: 'department',
                component: Department
            },
            {
                path: '/system/permission',
                name: 'permission',
                component: Permission
            },
            {
                path: '/myself/toDoTasks',
                name: 'toDoTasks',
                component: ToDoTasks
            },
            {
                path: '/myself/leaveBill',
                name: 'leaveBill',
                component: LeaveBill
            },
            {
                path: '/myself/reimbursementBill',
                name: 'reimbursementBill',
                component: ReimbursementBill
            },
            {
                path: '/myself/resign',
                name: 'resign',
                component: Resign
            },
            {
                path: '/myself/myDepartment',
                name: 'myDepartment',
                component: MyDepartment
            },
            {
                path: '/product/productList',
                name: 'productList',
                component: ProductList
            },
            {
                path: '/product/conceptual',
                name: 'conceptual',
                component: Conceptual
            },
            {
                path: '/developed/developedCenter',
                name: 'developedCenter',
                component: DevelopedCenter
            },
            {
                path: '/business/producePlan',
                name: 'producePlan',
                component: ProducePlan
            },
            {
                path: '/business/salesOrder',
                name: 'salesOrder',
                component: salesOrder
            },
            {
                path: '/business/outboundBill',
                name: 'outboundBill',
                component: OutboundBill
            },
            {
                path: '/finance/salary',
                name: 'salary',
                component: Salary
            },
            {
                path: '/finance/financeInfo',
                name: 'financeInfo',
                component: FinanceInfo
            },
        ]
    },
]

const router = createRouter({
    history: createWebHashHistory(process.env.BASE_URL),
    routes
})

router.beforeEach(
    (to, from, next) => {
        if (to.meta.requiredLogin != false) {
            const user = store.state.user
            console.log("跳转路由检查User", user)
            if (user != null) {
                next()
            } else {
                //判断cookies是否存在账号密码信息,若存在则登录并更新User信息
                if (cookies.isKey("usercode")
                    && cookies.isKey("password")) {
                    axios.post('/user/login', {}).then(rs => {
                        console.log("cookie自动登录结果", rs)
                        if (rs.code == 200) {
                            store.commit('setUser', rs.data)
                            router.push('/home')
                        } else {
                            message.error("当前登录已过期，请重新登陆")
                            next("/")
                        }
                    })
                } else {
                    message.error("当前登录已过期，请重新登陆")
                    next("/")
                }
            }
        } else {
            next()
        }
    }
)

export default router
